package mp3tagger.views;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.text.html.ListView;

import mp3model.Mp3Model;
import mp3tagger.Activator;
import mp3tagger.ids.Mp3TaggerIds;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.List;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;

public class Mp3List extends ViewPart implements PropertyChangeListener {

	private ListViewer listViewer;

	public Mp3List() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void createPartControl(Composite parent) {
		parent.setLayout(new GridLayout(1, false));
		
		final Button btnBrowse = new Button(parent, SWT.NONE);
		btnBrowse.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				DirectoryDialog dirDl = new DirectoryDialog(btnBrowse.getShell());
				dirDl.setText("Select folder");
				String path = dirDl.open();
				if ( path != null && path.length() > 0)
					Activator.getDefault().getMp3ListController().setSourceFolder( path);
			}
		});
		btnBrowse.setText("Browse...");
		
		Activator.getDefault().getMp3ListController().addPropertyChangeListener( this);
		Activator.getDefault().registerListener();
		
		listViewer = new ListViewer(parent, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
		
		initListViewer();
		// TODO Auto-generated method stub
		
	}

	private void initListViewer() {
		List list = listViewer.getList();
		GridData gd_list = new GridData(GridData.FILL_VERTICAL);
		gd_list.widthHint = 350;
		list.setLayoutData(gd_list);
		
		getSite().setSelectionProvider( listViewer);
		listViewer.setContentProvider(new ArrayContentProvider());
		listViewer.setLabelProvider(new ILabelProvider() {
			@Override
			public void removeListener(ILabelProviderListener listener) { }
			
			@Override
			public boolean isLabelProperty(Object element, String property) { return false; }
			
			@Override
			public void dispose() { }
			
			@Override
			public void addListener(ILabelProviderListener listener) { }
			
			@Override
			public String getText(Object element) {
				if ( !(element instanceof Mp3Model))
					return null;
				
				Mp3Model model = (Mp3Model)element;
				return model.getFilename();
				
			}
			
			@Override
			public Image getImage(Object element) { return null; }
		});
		
	}

	@Override
	public void setFocus() {

	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if( evt.getPropertyName().equals(Mp3TaggerIds.ITEMS_LOADED))
			updateTreeViewer(evt.getNewValue());
	}

	private void updateTreeViewer(Object newValue) {
		listViewer.setInput( newValue);
	}

}
